package com.ray.activiti.service.impl;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.ray.activiti.common.Constant;
import com.ray.activiti.common.Constant.NodeType;
import com.ray.activiti.common.utils.ActUtils;
import com.ray.activiti.common.utils.StringUtils;
import com.ray.activiti.service.ExtendActNodesetService;
import com.ray.activiti.table.entity.ExtendActNodeset;
import com.ray.activiti.table.mapper.ExtendActNodesetMapper;
import com.zjhc.flyhong.common.function.exception.BussinessException;
import com.zjhc.flyhong.common.util.UUIDUtil;

/**
 * 
 * @Auther ray  qray686898@163.com
 * @Date 2017/12/28
 * @代码参考或来自  https://github.com/huangxianyuan/hxyFrame
 *
 */
@Service
public class ExtendActNodesetServiceImpl implements ExtendActNodesetService {
	@Autowired
	private ExtendActNodesetMapper extendActNodesetMapper;

	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public ExtendActNodeset queryObject(String id){
		return extendActNodesetMapper.queryObject(id);
	}
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public List<ExtendActNodeset> queryList(Map<String, Object> map){
		return extendActNodesetMapper.queryList(map);
	}
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public int queryTotal(Map<String, Object> map){
		return extendActNodesetMapper.queryTotal(map);
	}

    @Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class) 
	public void delete(String id){
		extendActNodesetMapper.delete(id);
	}
	
    @Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class)
	public void deleteBatch(String[] ids){
		extendActNodesetMapper.deleteBatch(ids);
	}

	
    @Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class)
	public ExtendActNodeset saveNode(ExtendActNodeset actNodeset) throws IOException {
		if(StringUtils.isEmpty(actNodeset.getModelId())){
			throw new BussinessException(-1,"模型id不能为空!");
		}
		if(StringUtils.isEmpty(actNodeset.getNodeId())){
			throw new BussinessException(-1,"节点id不能为空!");
		}
		if(StringUtils.isEmpty(actNodeset.getNodeType())){
			throw new BussinessException(-1,"节点类型不能为空!");
		}
		//节点类型为审批节点
		if(actNodeset.getNodeType().equals(NodeType.EXAMINE.getValue())){
			//设置会签取消会签
			if(Constant.ActAction.MULIT.getValue().equals(actNodeset.getNodeAction())){
				try {
					ActUtils.setMultiInstance(actNodeset.getModelId(),actNodeset.getNodeId());
				} catch (Exception e) {
					throw new BussinessException(-1,"设置会签失败");
				}
			}else {
				try {
					ActUtils.clearMultiInstance(actNodeset.getModelId(),actNodeset.getNodeId());
				} catch (Exception e) {
					throw new BussinessException(-1,"取消会签失败");
				}
			}
			if(StringUtils.isEmpty(actNodeset.getId())){
				//保存节点信息
                actNodeset.setId(UUIDUtil.getUUID());
				extendActNodesetMapper.save(actNodeset);
			}else {
				//更新
				//保存节点信息
				extendActNodesetMapper.update(actNodeset);	
			}
		}
		//分支条件连线
		if(actNodeset.getNodeType().equals(NodeType.LINE.getValue())){
			//保存
			if(StringUtils.isEmpty(actNodeset.getId())){
				//保存节点信息
                actNodeset.setId(UUIDUtil.getUUID());
				extendActNodesetMapper.save(actNodeset);
			}else {
				//更新
				//保存节点信息
				extendActNodesetMapper.update(actNodeset);
			}
			//添加条件
			ActUtils.setSequenceFlowCondition(actNodeset.getModelId(),actNodeset.getNodeId(),actNodeset.getCondition());
		}
		//节点类型为结束
		if(actNodeset.getNodeType().equals(NodeType.END.getValue())){
			if(StringUtils.isEmpty(actNodeset.getId())){
				//保存节点信息
				actNodeset.setId(UUIDUtil.getUUID());
				extendActNodesetMapper.save(actNodeset);
			}else {
				//更新
				extendActNodesetMapper.update(actNodeset);
			}
		}
		return actNodeset;
	}

	
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
    public ExtendActNodeset queryByNodeId(String nodeId) {
        return extendActNodesetMapper.queryByNodeId(nodeId);
    }

	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public ExtendActNodeset queryByNodeIdModelId(String nodeId, String modelId) {
		return extendActNodesetMapper.queryByNodeIdModelId(nodeId,modelId);
	}


	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public ExtendActNodeset queryStartNodeByModelId(String modelId) {
		return extendActNodesetMapper.queryStartNodeByModelId(modelId);
	}
}
